#include "av_pkt_buf.h"

#include <assert.h>
#include <stdlib.h>

extern av_pkt_buf_desc_t pkt_buf_desc_zerotime_hi3516;
av_pkt_buf_desc_t *g_pkt_buf_desc_array[AV_PKT_BUF_TYPE_MAX + 1] = {
    &pkt_buf_desc_zerotime_hi3516, NULL,
};

av_pkt_buf_manager_t *av_pkt_buf_alloc_manager(av_pkt_buf_type_t type) {
    av_pkt_buf_desc_t *desc = g_pkt_buf_desc_array[type];

    av_pkt_buf_manager_t *m = NULL;

    if (desc->init_func) {
        m = desc->init_func(desc);
        if (m == NULL) {
            return NULL;
        }
    }

    return m;
}

av_pkt_buf_t *av_pkt_buf_alloc(av_pkt_buf_manager_t *m) {
    return m->desc->alloc_func(m);
}

void av_pkt_buf_free(av_pkt_buf_t *buf) { return buf->m->desc->free_func(buf); }

int av_pkt_buf_add_data(av_pkt_buf_t *buf, char *data, size_t len) {
    return buf->m->desc->add_data_func(buf, data, len);
}

char *av_pkt_buf_data(av_pkt_buf_t *buf) {
    return buf->m->desc->data_func(buf);
}

size_t av_pkt_buf_data_len(av_pkt_buf_t *buf) {
    return buf->m->desc->data_len_func(buf);
}

void av_pkt_buf_set_data_len(av_pkt_buf_t *buf, size_t len) {
    buf->m->desc->set_data_len_func(buf, len);
}

/*
 * If `buf` is the first pakcet of one frame, return 1, else 0
 *
 * return -1, if not supported
 */
int av_pkt_buf_is_sof(av_pkt_buf_t *buf) {
    return buf->m->desc->is_sof_func(buf);
}

/*
 * If `buf` is the last pakcet of one frame, return 1, else 0
 *
 * return -1, if not supported
 */
int av_pkt_buf_is_eof(av_pkt_buf_t *buf) {
    return buf->m->desc->is_eof_func(buf);
}

uint16_t av_pkt_buf_grp(av_pkt_buf_t *buf) {
    return buf->m->desc->grp_func(buf);
}

uint32_t av_pkt_buf_seq(av_pkt_buf_t *buf) {
    return buf->m->desc->seq_func(buf);
}
